package com.kaizzi.data.ppt.template;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.NameDouble;
import com.kaizzi.data.common.been.SeriesData;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.jdl.service.LogService;
import com.kaizzi.data.ppt.TemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xslf.usermodel.XSLFChart;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/27
 */
@Slf4j
@Component
public class Template25 {

    @Resource
    private TemplateUtil templateUtil;
    @Resource
    private LogService logService;

    public void text(List<XSLFTextParagraph> textParagraphs,JsonNode data) {
        if(textParagraphs==null||textParagraphs.size()==0){
            return;
        }
        for (XSLFTextParagraph textParagraph : textParagraphs) {
            if(textParagraph==null){
                continue;
            }
            List<XSLFTextRun> textRuns = textParagraph.getTextRuns();
            if(textRuns==null){
                continue;
            }
            for (XSLFTextRun textRun : textRuns) {
                if (textRun == null || textRun.getXmlObject()  == null){
                    return;
                }
                try {
                    if (textRun.getRawText().equals("16KEY-1")){
                        String squareData = data.get("title1").asText();
                        templateUtil.text(textParagraph,squareData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-3")){
                        String teamData = data.get("title2").asText();
                        templateUtil.text(textParagraph,teamData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-5")){
                        String squareData = data.get("title3").asText();
                        templateUtil.text(textParagraph,squareData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-7")){
                        String teamData = data.get("title4").asText();
                        templateUtil.text(textParagraph,teamData,false);
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-9")){
                        String title = data.get("title").asText();
                        textRun.setText(title);
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-11")){
                        textRun.setText(data.get("title11").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-12")){
                        textRun.setText(data.get("title31").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-13")){
                        textRun.setText(data.get("title21").asText());
                        break;
                    }
                    if (textRun.getRawText().equals("16KEY-14")){
                        textRun.setText(data.get("title41").asText());
                        break;
                    }
                    break;
                }catch (Exception e){
                    logService.setDataLogStatus("ppt-error");
                    log.error("page:{},text Exception:{}",25,e);
                }
            }
        }
    }

    public void barChart(CTPlotArea plot, JsonNode data, XSSFWorkbook workbook, XSSFSheet sheet, XSLFChart chart) {
        String jsonKey;
        String jsonKey1;
        switch (plot.getBarChartList().get(0).getSerList().get(0).getVal().getNumRef().getNumCache().getPtArray(0).getV()){
            case "205":
                jsonKey = "thisDetailCount";
                jsonKey1 = "thisDetailUser";
                break;
            case "958":
                jsonKey = "thisSquareCount";
                jsonKey1 = "thisSquareUser";
                break;
            case "251":
                jsonKey = "lastDetailCount";
                jsonKey1 = "lastDetailUser";
                break;
            case "1052":
                jsonKey = "lastSquareCount";
                jsonKey1 = "lastSquareUser";
                break;
            default:
                jsonKey = "";
                jsonKey1 = "";
                break;
        }
        List<SeriesData> barData = getBarData(data, jsonKey, jsonKey1);
        templateUtil.barChart(plot,barData,workbook,sheet,chart);
    }

    private static List<SeriesData> getBarData(JsonNode jsonNode, String jsonKey, String jsonKey1){

        List<SeriesData> seriesDatas = new ArrayList<>();
        List<NameDouble> nameDouble = getNameDouble(jsonNode.get(jsonKey));
        List<NameDouble> nameDouble1 = getNameDouble(jsonNode.get(jsonKey1));

        SeriesData newUserSeriesData1 = new SeriesData("访问次数PV",nameDouble,"");
        SeriesData newUserSeriesData2 = new SeriesData("累积访问人数UV",nameDouble1,"");
        seriesDatas.add(newUserSeriesData1);
        seriesDatas.add(newUserSeriesData2);
        return seriesDatas;
    }

    private static List<NameDouble> getNameDouble(JsonNode data){
        List<NameDouble> nameDoubles = new ArrayList<>();
        for (JsonNode node : data) {
            NameDouble nameDouble = new NameDouble(node.get("tempName").asText(),node.get("tempCount").asDouble());
            nameDoubles.add(nameDouble);
        }
        return nameDoubles;
    }

}
